﻿using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using MS.InformationManagement.ContentHub.CustomFields;
using MS.InformationManagement.DAL.DocumentCenter;
namespace MS.InformationManagement.ContentHub.WebControls
{
    public class AttachedDocumentsFieldControl : BaseFieldControl
    {
        private const string RENDERING_TEMPLATE = "AttachedDocumentsFieldControl";

        private string _value;

        protected Repeater rptAttachedDocuments;

        #region [Base Members]

        protected override string DefaultTemplateName
        {
            get { return RENDERING_TEMPLATE; }
        }

        public override string DisplayTemplateName
        {
            get
            {
                return RENDERING_TEMPLATE;
            }
        }

        public override object Value
        {
            get
            {
                EnsureChildControls();
                //SetValue();
                return _value;
            }
            set
            {
                EnsureChildControls();
                _value = value.ToString();
                //LoadData();
            }
        }

        protected override void CreateChildControls()
        {
            if (Field == null || ControlMode == SPControlMode.Invalid)
                return;
            base.CreateChildControls();
            rptAttachedDocuments = TemplateContainer.FindControl("rptAttachedDocuments") as Repeater;
            if (base.ItemFieldValue != null)
            {
                var value = new AttachedDocumentsFieldValue(base.ItemFieldValue.ToString());
                rptAttachedDocuments.DataSource = value.AttachedDocuments.ToList();
            }
            else {
                rptAttachedDocuments.DataSource = new List<string>();
            }

            rptAttachedDocuments.DataBind();

        }
 
        #endregion
    }
}
